package com.qyer.commons.concurrent.assemblingline.creator;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.qyer.commons.concurrent.assemblingline.ProcessMode;

import java.util.concurrent.atomic.AtomicLong;

/**
 * User: Z J Wu Date: 2016/06/12 Time: 18:55 Package: com.qyer.commons.concurrent.assemblingline
 * .creator
 */
public class BasicStatistic {

  @JsonProperty
  private ProcessMode mode;
  @JsonIgnore
  private AtomicLong successCount;
  @JsonIgnore
  private AtomicLong failureCount;
  @JsonIgnore
  private AtomicLong totalTimeCost;

  public BasicStatistic() {
    this.successCount = new AtomicLong(0);
    this.failureCount = new AtomicLong(0);
    this.totalTimeCost = new AtomicLong(0);
  }

  public BasicStatistic(ProcessMode mode) {
    this.mode = mode;
    this.successCount = new AtomicLong(0);
    this.failureCount = new AtomicLong(0);
    this.totalTimeCost = new AtomicLong(0);
  }

  @JsonProperty("success")
  public long getSuccess() {
    return successCount.get();
  }

  @JsonProperty("failure")
  public long getFailure() {
    return failureCount.get();
  }

  public AtomicLong getSuccessCount() {
    return successCount;
  }

  public AtomicLong getFailureCount() {
    return failureCount;
  }

  public AtomicLong getTotalTimeCost() {
    return totalTimeCost;
  }

  public void setSuccessCount(AtomicLong successCount) {
    this.successCount = successCount;
  }

  public void setFailureCount(AtomicLong failureCount) {
    this.failureCount = failureCount;
  }

  public void setTotalTimeCost(AtomicLong totalTimeCost) {
    this.totalTimeCost = totalTimeCost;
  }

  public ProcessMode getMode() {
    return mode;
  }

  public void setMode(ProcessMode mode) {
    this.mode = mode;
  }

  @JsonProperty("avg_time_cost")
  public long getAVGTimeCost() {
    return totalTimeCost.get() / (successCount.get() + failureCount.get());
  }
}
